package ciyuan.Item.Tools;

import ciyuan.Constants.ItemQuality;
import ciyuan.Item.ItemGroups;
import ciyuan.Item.TypeInterface.ALLItemType;
import ciyuan.Structure.MyFile.StructureBuilder;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Direction;
import net.minecraft.util.Hand;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.RayTraceContext;
import net.minecraft.util.math.RayTraceResult;
import net.minecraft.util.text.StringTextComponent;
import net.minecraft.world.World;

/**
 * @author 150149
 */
public class Placer extends Item implements ALLItemType {


    public Placer() {
        super(new Properties().tab(ItemGroups.Tools));
    }

    @Override
    public ActionResult<ItemStack> use(World world, PlayerEntity playerEntity, Hand hand) {
        ItemStack itemstack = playerEntity.getItemInHand(hand);
        RayTraceResult raytraceresult = getPlayerPOVHitResult(world, playerEntity,  RayTraceContext.FluidMode.SOURCE_ONLY);
        BlockRayTraceResult blockraytraceresult = (BlockRayTraceResult)raytraceresult;
        if (raytraceresult.getType() == RayTraceResult.Type.BLOCK) {
            BlockPos blockpos = blockraytraceresult.getBlockPos();
            Direction direction = blockraytraceresult.getDirection();
            BlockPos blockpos1 = blockpos.relative(direction);
            if (world.mayInteract(playerEntity,blockpos) && playerEntity.mayUseItemAt(blockpos1,direction,itemstack)) {
                StructureBuilder.onPlace(blockpos,world, StructureBuilder.tempsave);
                playerEntity.sendMessage(new StringTextComponent("已放置结构"),playerEntity.getUUID());
            }
        }
        return ActionResult.pass(itemstack);
    }

    @Override
    public ItemQuality getQuality() {
        return ItemQuality.NORMAL;
    }
}
